package junior.util.string;

import org.springframework.lang.Nullable;

public class Strings {
    
    public static boolean isEmpty(@Nullable String str) {
        return str == null || str.isEmpty();
    }
    
    public static boolean hasText(@Nullable CharSequence str) {
        return str != null && str.length() > 0 && containsText(str);
    }
    
    public static boolean hasText(@Nullable String str) {
        return str != null && !str.isEmpty() && containsText(str);
    }
    
    private static boolean containsText(CharSequence str) {
        int strLen = str.length();
        
        for(int i = 0; i < strLen; ++i) {
            if (!Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        
        return false;
    }
    
    public static String cut(String s, int maxLength) {
        if (s.length() > maxLength) {
            return s.substring(0, maxLength - 3) + "...";
        } else {
            return s;
        }
    }
    
}
